#include "StdAfx.h"

TutorialNotify::TutorialNotify()
{
	m_highLight = false;
	m_fadedIn = true;
	m_fadeStart = 0;
	m_position.x = (float)(sD3D.GetSize().x / 2 - 64);
	m_position.y = (float)(sD3D.GetSize().y - 64) - 58;
	m_size.x = 128;
	m_size.y = 64;
	m_baseTexture = CTextureHandler::GetInstance()->GetTexture("Interface\\TutorialFrame\\UI-Help-Alert.blp");

	for(ui32 i = 0; i < 4; ++i)
	{
		m_baseVertices[i].dwcolor = 0xFFFFFFFF;
		m_highVertices[i].dwcolor = 0xFFFFFFFF;
	}

	m_baseVertices[0].z = m_baseVertices[1].z = m_baseVertices[2].z = m_baseVertices[3].z = 0.0f;
	m_baseVertices[0].rhw = m_baseVertices[1].rhw = m_baseVertices[2].rhw = m_baseVertices[3].rhw = 1.0f;
	m_baseVertices[0].x = (float)(sD3D.GetSize().x / 2 - 64);
	m_baseVertices[0].y = (float)(sD3D.GetSize().y - 64 - 58);
	m_baseVertices[0].u = 0.01f;
	m_baseVertices[0].v = 0.01f;

	m_baseVertices[1].x = (float)(sD3D.GetSize().x / 2 + 64);
	m_baseVertices[1].y = (float)(sD3D.GetSize().y - 64 - 58);
	m_baseVertices[1].u = 0.99f;
	m_baseVertices[1].v = 0.01f;

	m_baseVertices[2].x = (float)(sD3D.GetSize().x / 2 + 64);
	m_baseVertices[2].y = (float)(sD3D.GetSize().y - 58);
	m_baseVertices[2].u = 0.99f;
	m_baseVertices[2].v = 0.5f;

	m_baseVertices[3].x = (float)(sD3D.GetSize().x / 2 - 64);
	m_baseVertices[3].y = (float)(sD3D.GetSize().y - 58);
	m_baseVertices[3].u = 0.01f;
	m_baseVertices[3].v = 0.5f;

	m_highVertices[0].z = m_highVertices[1].z = m_highVertices[2].z = m_highVertices[3].z = 0.0f;
	m_highVertices[0].rhw = m_highVertices[1].rhw = m_highVertices[2].rhw = m_highVertices[3].rhw = 1.0f;
	m_highVertices[0].x = (float)(476);
	m_highVertices[0].y = (float)(708 - 30 - 62);
	m_highVertices[0].u = 0.43f;
	m_highVertices[0].v = 0.56f;

	m_highVertices[1].x = (float)(536);
	m_highVertices[1].y = (float)(708 - 30 - 62);
	m_highVertices[1].u = 107.0f / 128.0f;
	m_highVertices[1].v = 0.56f;


	m_highVertices[2].x = (float)(536);
	m_highVertices[2].y = (float)(738 - 62);
	m_highVertices[2].u = 107.0f / 128.0f;
	m_highVertices[2].v = 0.99f;

	m_highVertices[3].x = (float)(476);
	m_highVertices[3].y = (float)(738 - 62);
	m_highVertices[3].u = 0.43f;
	m_highVertices[3].v = 0.99f;
}

void TutorialNotify::Resize()
{
	m_position.x = (float)(sD3D.GetSize().x / 2 - 64);
	m_position.y = (float)(sD3D.GetSize().y - 64) - 58;
	m_baseVertices[0].z = m_baseVertices[1].z = m_baseVertices[2].z = m_baseVertices[3].z = 0.0f;
	m_baseVertices[0].rhw = m_baseVertices[1].rhw = m_baseVertices[2].rhw = m_baseVertices[3].rhw = 1.0f;
	m_baseVertices[0].x = (float)(sD3D.GetSize().x / 2 - 64);
	m_baseVertices[0].y = (float)(sD3D.GetSize().y - 64 - 58);
	m_baseVertices[0].u = 0.01f;
	m_baseVertices[0].v = 0.01f;

	m_baseVertices[1].x = (float)(sD3D.GetSize().x / 2 + 64);
	m_baseVertices[1].y = (float)(sD3D.GetSize().y - 64 - 58);
	m_baseVertices[1].u = 0.99f;
	m_baseVertices[1].v = 0.01f;

	m_baseVertices[2].x = (float)(sD3D.GetSize().x / 2 + 64);
	m_baseVertices[2].y = (float)(sD3D.GetSize().y - 58);
	m_baseVertices[2].u = 0.99f;
	m_baseVertices[2].v = 0.5f;

	m_baseVertices[3].x = (float)(sD3D.GetSize().x / 2 - 64);
	m_baseVertices[3].y = (float)(sD3D.GetSize().y - 58);
	m_baseVertices[3].u = 0.01f;
	m_baseVertices[3].v = 0.5f;

	m_highVertices[0].z = m_highVertices[1].z = m_highVertices[2].z = m_highVertices[3].z = 0.0f;
	m_highVertices[0].rhw = m_highVertices[1].rhw = m_highVertices[2].rhw = m_highVertices[3].rhw = 1.0f;
	m_highVertices[0].x = (float)(sD3D.GetSize().x / 2 - 34);
	m_highVertices[0].y = (float)(sD3D.GetSize().y - 60 - 60);
	m_highVertices[0].u = 0.43f;
	m_highVertices[0].v = 0.56f;

	m_highVertices[1].x = (float)(sD3D.GetSize().x / 2 + 26);
	m_highVertices[1].y = (float)(sD3D.GetSize().y - 60 - 60);
	m_highVertices[1].u = 107.0f / 128.0f;
	m_highVertices[1].v = 0.56f;


	m_highVertices[2].x = (float)(sD3D.GetSize().x / 2 + 26);
	m_highVertices[2].y = (float)(sD3D.GetSize().y - 60);
	m_highVertices[2].u = 107.0f / 128.0f;
	m_highVertices[2].v = 0.99f;

	m_highVertices[3].x = (float)(sD3D.GetSize().x / 2 - 34);
	m_highVertices[3].y = (float)(sD3D.GetSize().y - 60);
	m_highVertices[3].u = 0.43f;
	m_highVertices[3].v = 0.99f;
}

void TutorialNotify::Draw()
{
	if(!m_visible)
		return;
	if(m_fadeStart)
	{
		if(m_fadeDir)
		{
			ui8 oldAlpha = m_baseVertices[0].dwcolor >> 24;
			float inc = (255.0f / 2000.0f) * (GetTickCount() - m_fadeStart);
			if((float)oldAlpha + inc >= 255)
			{
				m_fadeStart = 0;
				m_fadedIn = true;
				oldAlpha = 255;
			}
			else
				oldAlpha += (ui8)inc;
			for(ui32 i = 0; i < 4; ++i)
			{
				m_baseVertices[i].dwcolor &= ~(ui32(0xFF) << 24);
				m_baseVertices[i].dwcolor |= (ui32(oldAlpha) << 24);
			}
		}
		else
		{
			ui8 oldAlpha = m_baseVertices[0].dwcolor >> 24;
			float inc = (255.0f / 2000.0f) * (GetTickCount() - m_fadeStart);
			if((float)oldAlpha - inc <= 0)
			{
				m_fadeStart = 0;
				m_fadedIn = false;
				m_visible = false;
				m_highLight = false;
				oldAlpha = 0;
			}
			else
				oldAlpha -= (ui8)inc;
			for(ui32 i = 0; i < 4; ++i)
			{
				m_baseVertices[i].dwcolor &= ~(ui32(0xFF) << 24);
				m_baseVertices[i].dwcolor |= (ui32(oldAlpha) << 24);
			}
		}
	}
	sD3D->SetFVF(VERTEX3FCRHWT);
	sD3D->SetTexture(0, *m_baseTexture);
	sD3D->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, m_baseVertices, sizeof(Vertex3FCRHWT));
	if(m_highLight)
		sD3D->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, m_highVertices, sizeof(Vertex3FCRHWT));
}

void TutorialNotify::OnMouseMove(ui32 x, ui32 y, bool leftDown, bool rightDown)
{
	if(!m_visible || !m_fadedIn)
		return;
	if(powf((float)x - sD3D.GetSize().x / 2 - 2, 2.0f) + powf((float)y - (sD3D.GetSize().y - 87), 2.0f) < powf(30, 2))
		m_highLight = true;
	else
		m_highLight = false;
}

void TutorialNotify::MouseActivity(bool left, Vertex2F pos, bool up)
{
	if(!m_visible || !m_fadedIn)
		return;
	if(up)
	{
		if(powf((float)pos.x - sD3D.GetSize().x / 2 - 2, 2.0f) + powf((float)pos.y - (sD3D.GetSize().y - 87), 2.0f) < powf(30, 2))
		{
			sTutorial.ShowTutorial();
			FadeOut();
		}
	}
}

void TutorialNotify::FadeIn()
{
	if(m_baseVertices[0].dwcolor >> 24 == 255)
		return;
	m_fadeStart = GetTickCount();
	for(ui32 i = 0; i < 4; ++i)
	{
		m_baseVertices[i].dwcolor &= ~(ui32(0xFF) << 24);
	}
	m_fadeDir = true;
	m_fadedIn = false;
}

void TutorialNotify::FadeOut()
{
	m_fadeStart = GetTickCount();
	m_fadeDir = false;
	m_fadedIn = false;
}